package com.fccommunity.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.fccommunity.R;
import com.fccommunity.base.BaseActivity;
import com.fccommunity.ui.fragment.GZFWFragment;
import com.fccommunity.ui.fragment.MineFragment;
import com.fccommunity.ui.fragment.QHBFragment;
import com.fccommunity.widget.FragmentTabHost;

/**
 * Created by yiwei on 16/4/16.
 */
public class MainActivity extends BaseActivity{

    private FragmentTabHost tabHost;
    private static long DOUBLE_CLICK_TIME = 0L;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }


    @Override
    protected void initTitle() {

    }

    private void initView() {
        tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        tabHost.setup(MainActivity.this, getSupportFragmentManager(), R.id.container);
        initTabHost();
        tabHost.setCurrentTab(0);
        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                int size = tabHost.getTabWidget().getTabCount();
            }
        });
        if (Build.VERSION.SDK_INT > 10) {
            tabHost.getTabWidget().setShowDividers(0);
        }
    }

    private void initTabHost() {
        MainTabs[] tabs = MainTabs.values();
        for (int i = 0; i < tabs.length; i++) {
            MainTabs mTab = tabs[i];
            TabHost.TabSpec tab = tabHost.newTabSpec(mTab.getTab());
            View indcator = getLayoutInflater().inflate(R.layout.layout_tab, null);
            TextView hostName = (TextView) indcator.findViewById(R.id.hostName);
            hostName.setText(mTab.getHostName());
            Drawable topDrawable = getResources().getDrawable(mTab.getDrawable());
            topDrawable.setBounds(0, 0 ,topDrawable.getIntrinsicWidth(), topDrawable.getIntrinsicHeight());
            hostName.setCompoundDrawables(null, topDrawable, null, null);
            tab.setIndicator(indcator);
            tab.setContent(new TabHost.TabContentFactory() {
                @Override
                public View createTabContent(String tag) {
                    return new View(MainActivity.this);
                }
            });

            tabHost.addTab(tab, mTab.getClzz(), null);
        }
    }

    public enum MainTabs {
        GZFW(0, "GZFW", R.drawable.select_tab_gzfw, "公租服务", GZFWFragment.class),
        QHB(1, "QHB", R.drawable.select_tab_qhb, "抢红包", QHBFragment.class),
        MINE(2, "MINE", R.drawable.select_tab_mine, "我的", MineFragment.class);

        private int id;
        private String tab;
        private int drawable;
        private String hostName;
        private Class<?> clzz;

        MainTabs(int id, String tab, int drawable, String hostName, Class<?> clzz) {
            this.id = id;
            this.tab = tab;
            this.drawable = drawable;
            this.hostName = hostName;
            this.clzz = clzz;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getTab() {
            return tab;
        }

        public void setTab(String tab) {
            this.tab = tab;
        }

        public int getDrawable() {
            return drawable;
        }

        public void setDrawable(int drawable) {
            this.drawable = drawable;
        }

        public String getHostName() {
            return hostName;
        }

        public void setHostName(String hostName) {
            this.hostName = hostName;
        }

        public Class<?> getClzz() {
            return clzz;
        }

        public void setClzz(Class<?> clzz) {
            this.clzz = clzz;
        }
    }

    public static Intent newIntent(Context context){
        Intent intent = new Intent(context, MainActivity.class);

        return intent;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - DOUBLE_CLICK_TIME) > 2000) {
                //showToast(getString(R.string.double_click_exit), Toast.LENGTH_SHORT);
                Toast.makeText(context, "再按一次退出", Toast.LENGTH_LONG).show();
                DOUBLE_CLICK_TIME = System.currentTimeMillis();
            } else {
                //((BbmmApplication)getApplication()).exitApp();
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
